package demo.clone;

import java.io.IOException;

public class TestClone {
    public static void main(String[] args) throws CloneNotSupportedException, IOException, ClassNotFoundException {
        Writer writer1 = new Writer(18, "二哥");
        Book book1 = new Book("编译原理", 100);
        writer1.setBook(book1);

        Writer writer2 = (Writer) writer1.clone();
        System.out.println("浅拷贝后：");
        System.out.println("writer1：" + writer1);
        System.out.println("writer2：" + writer2);

        Writer writer3 = (Writer) writer1.deepClone();
        System.out.println("深拷贝后：");
        System.out.println("writer1：" + writer1);
        System.out.println("writer3：" + writer3);

        writer2.setName("三妹");
        System.out.println("调整了 writer2 的 name 后：");
        System.out.println("writer1：" + writer1);
        System.out.println("writer2：" + writer2);

        Book book2 = writer2.getBook();
        book2.setBookName("永恒的图灵");
        book2.setPrice(70);
        System.out.println("writer2.book 变更后：");
        System.out.println("writer1：" + writer1);
        System.out.println("writer2：" + writer2);

        writer3.setName("阿甘");
        Book book3 = writer3.getBook();
        book3.setBookName("深入理解Java虚拟机");
        book3.setPrice(89);
        System.out.println("调整了 writer3 的 name 并且 writer3.book 变更后：");
        System.out.println("writer1：" + writer1);
        System.out.println("writer3：" + writer3);
    }
}
